package 多线程;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class reentrantLockTest {
    
    static ReentrantLock lock = new ReentrantLock();

    static Condition c1 = lock.newCondition();

    static Condition c2 = lock.newCondition();

    public static void main(String[] args) throws InterruptedException{
        // lockInterrupt();
        // timeOutLock();
        conditionTest();
    }
    public static void conditionTest() throws InterruptedException {
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "...have...lock");
            try {
                // 和synchronized当中的wait-notify一样
                c1.await();
                System.out.println(Thread.currentThread().getName() + "...aquire...lock");
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "...have...lock");
            try {
                c2.await();
                System.out.println(Thread.currentThread().getName() + "...aquire...lock");
                Thread.sleep(1000L);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "...have...lock");
            try {
                c1.signal();
                c2.signal();
                System.out.println(Thread.currentThread().getName() + "...aquire..lock");
                Thread.sleep(1000L);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }
    public static void lockInterrupt() throws InterruptedException{
        Thread t1 = new Thread(() -> {
            try {
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(Thread.currentThread().getName() + "...interrupted...");
                return;
            }
            try {
                System.out.println(Thread.currentThread().getName() + "...hava...lock...");
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "...hava...lock...");
        t1.start();
        try {
            Thread.sleep(1000L);
            t1.interrupt();
            System.out.println(Thread.currentThread().getName() + "...start...interrupt...");
        } finally {
            lock.unlock();
        }
    }
    public static void timeOutLock() throws InterruptedException{
        Thread t1 = new Thread(() -> {
            try {
                if(!lock.tryLock(2, TimeUnit.SECONDS)){
                    System.out.println(Thread.currentThread().getName() + "...try...lock...failed...");
                    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try{
                System.out.println(Thread.currentThread().getName() + "...hava...lock...");
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "...hava...lock...");
        t1.start();
        try{
            Thread.sleep(1000L);
        } finally {
            lock.unlock();
        }
    }
}
